import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/modle/person/bloc/my_tx_bloc.dart';
import 'package:shopping_group/modle/person/entity/get_money_record_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/widget_utils.dart';

class CashMoneyPage extends StatefulWidget {
  CashMoneyPage({Key key}) : super(key: key);

  @override
  _CashMoneyPageState createState() => _CashMoneyPageState();
}

class _CashMoneyPageState extends State<CashMoneyPage>
    with AutomaticKeepAliveClientMixin {
  List<GetMoneyRecordEntity> _listEntity = [];
  ScrollController _scrollController = ScrollController();
  MyTxBloc _bloc;
  StreamSubscription _refresSubscription;
  @override
  void initState() {
    super.initState();
    _bloc = MyTxBloc();
    _bloc.initDate.add(null);
    _initListener();
    _refresSubscription = eventBus.on<RefreshEvent>().listen((event) {
      _bloc.initDate.add(null);
    });
  }

  _initListener() {
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _bloc.moreDate.add(_scrollController);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      child: _showlistView(),
    );
  }

  Widget _showlistView() {
    return StreamBuilder(
      stream: _bloc.sendData,
      initialData: _listEntity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is List) {
          _listEntity = object;
        }
        int count = _listEntity.length;
        if (0 != count) {
          return Container(
            decoration: new BoxDecoration(
              color: Colors.white,
            ),
            child: AnimationLimiter(
              child: RefreshIndicator(
                onRefresh: _pullToRefresh,
                child: ListView.builder(
                  controller: _scrollController,
                  shrinkWrap: true,
                  padding: const EdgeInsets.only(top: 4),
                  itemCount: _listEntity.length,
                  itemBuilder: buildItem,
                ),
              ),
            ),
          );
        } else {
          return Container(
            alignment: Alignment.center,
            child: WidgetUtils.emptyView(
              contexts,
            ),
          );
        }
      },
    );
  }

  //下拉刷新,必须异步async不然会报错
  Future _pullToRefresh() async {
    _listEntity.clear();
    _bloc.initDate.add(null);
    return null;
  }

  //ListView的Item
  Widget buildItem(BuildContext context, int index) {
    return Container(
      // margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: _xunchaItem(_listEntity[index]),
    );
  }

  Widget _xunchaItem(GetMoneyRecordEntity entity) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.transparent,
      ),
      alignment: FractionalOffset.centerLeft,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(
            height: 4,
          ),
          WidgetUtils.itmeStyleView('提现时间', entity.createDate,
              contentAlignment: Alignment.centerRight,
              isExpanded: false,
              isContentExpanded: true,
              textColor: Colours.gray_91,
              contentColor: Colours.gray_91,
              verticalPadding: 4),
          WidgetUtils.itmeStyleView(
              '提代理额¥ ${entity.custGetVal}', entity.stateName,
              contentAlignment: Alignment.centerRight,
              isExpanded: false,
              isContentExpanded: true,
              textColor: Colours.gray_91,
              contentColor: Colours.themeColor,
              verticalPadding: 4),
          SizedBox(
            height: 4,
          ),
          Padding(
            padding: EdgeInsets.only(left: 12),
            child: WidgetUtils.getTextColorView(
              '${entity.bankTypeName}【${BaseConstant.name}】 ${entity.bankNo}',
              Colours.gray_91,
              Dimens.FONT_SIZE_16,
            ),
          ),
          SizedBox(
            height: 4,
          ),
          WidgetUtils.drawCodeDivider(height: 1),
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    _refresSubscription?.cancel();
    super.dispose();
  }
}
